﻿using OA.Core.Attribute;
using OA.Core.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.Core
{
    public class MyEnum
    {
        /// <summary>
        /// 返回类型名称
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetNameByValue(int value, Type type)
        {
            foreach (string name in Enum.GetNames(type))
            {
                if (value == (int)Enum.Parse(type, name))
                {
                    return name;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 返回描述
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetEnumNameByValue(long value, Type type)
        {
            foreach (var item in type.GetEnumDescriptionAndValue())
            {
                if (item.Key == value)
                {
                    return item.Value;
                }
            }
            return string.Empty;
        }

         
        public static List<MySelectListItem> EnumToSelectList(Type type)
        {
            List<MySelectListItem> list = new List<MySelectListItem>();
            foreach (var item in type.GetEnumDescriptionAndValue())
            {
                list.Add(new MySelectListItem() { Value = item.Key.ToString(), Text = item.Value });
            }
            return list;
        }

        public static string GetCheckBoxListTextByValue(int value, Type type)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in type.GetEnumDescriptionAndValue())
            {
                if ((item.Key & value)== item.Key)
                {
                    sb.Append(item.Value+",");
                }
            }
            if (sb.Length > 0) sb.Length = sb.Length - 1;
            return sb.ToString();
        }
    }
}
